package yxy.game.pm2.module.active.progress;

import java.util.HashMap;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;

public class RankProgressArea extends AreaModule {

	// bean===========================================================================

	private int type; // 哪个排行榜
	private HashMap<Integer, RankProgress> rpmap = new HashMap<Integer, RankProgress>();

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public HashMap<Integer, RankProgress> getRpmap() {
		return rpmap;
	}

	public void setRpmap(HashMap<Integer, RankProgress> rpmap) {
		this.rpmap = rpmap;
	}

	// func===========================================================================

	public static int ConquestRank = 0; // 征战排行榜
	public static int TowerRank = 1; // 修炼塔排行榜
	public static int PowerRank = 3; // 战力排行榜

	private static RankProgressArea create(int areaId, int type) {
		RankProgressArea module = new RankProgressArea();
		module.setAreaId(areaId);
		module.setType(type);
		module.set();
		return module;
	}

	// redis==========================================================================
	private static String modulekey(int areaid, int type) {
		String modulename = String.format("RANKPROGRESS:%s", type);
		return key(areaid, modulename);
	}

	public void set() {
		try {
			setv(modulekey(getAreaId(), getType()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	static public RankProgressArea get(AreaBean areaBean, int type) {
		try {
			RankProgressArea module = getv(modulekey(areaBean.getAreaId(), type));
			if (module == null) {
				module = create(areaBean.getAreaId(), type);
			}
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}

	static public RankProgressArea get(String playerCode, int type) {
		AreaBean areaBean = AreaModule.area(playerCode);
		return get(areaBean, type);
	}

	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", modulekey(getAreaId(), getType()));
	}

}
